package routes

import (
	"net/http"
	"simple-crm/config"
	"simple-crm/controllers"
	"simple-crm/middleware"
	"simple-crm/services"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// SetupPageRoutes 设置页面路由
func SetupPageRoutes(r *gin.Engine, db *gorm.DB, cfg *config.Config, permissionMiddleware *middleware.PermissionMiddleware) {
	// 静态文件
	r.Static("/static", "./static")

	// 登录页面（公开）
	r.GET("/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "auth/login.html", gin.H{
			"title": "登录",
		})
	})

	// 根路径重定向到登录页
	r.GET("/", func(c *gin.Context) {
		c.Redirect(http.StatusFound, "/login")
	})

	// 仪表板（客户端验证）
	r.GET("/dashboard", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index/dashboard.html", gin.H{
			"title": "仪表板",
		})
	})

	// 需要认证的页面路由
	pages := r.Group("/")
	pages.Use(middleware.AuthMiddleware(db, cfg))
	{
		// 客户管理页面
		setupCustomerPages(pages, db)

		// 合作机构管理页面
		setupPartnerPages(pages, db)

		// 推送管理页面
		setupPushPages(pages, db, permissionMiddleware, cfg)

		// 调试页面
		pages.GET("/debug", func(c *gin.Context) {
			c.HTML(http.StatusOK, "system/debug.html", gin.H{
				"title": "权限调试",
				"User":  c.MustGet("user"),
			})
		})
	}
}

// setupCustomerPages 设置客户管理页面路由
func setupCustomerPages(pages *gin.RouterGroup, db *gorm.DB) {
	// 客户管理SPA页面
	pages.GET("/customers", middleware.RequireAnyPermission(db, []string{"customer.view", "customer.create", "customer.update", "customer.delete"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "customers/index.html", gin.H{
			"title":       "客户管理",
			"user":        c.MustGet("user"),
			"CurrentPage": "customers",
		})
	})

	// 客户详情页面
	pages.GET("/customers/:id", middleware.RequireAnyPermission(db, []string{"customer.view", "customer.create", "customer.update", "customer.delete"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "customers/show.html", gin.H{
			"title":       "客户详情",
			"user":        c.MustGet("user"),
			"CurrentPage": "customer-detail",
		})
	})

	// 客户编辑页面
	pages.GET("/customers/:id/edit", middleware.RequireAnyPermission(db, []string{"customer.update"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "customers/edit.html", gin.H{
			"title":       "编辑客户",
			"user":        c.MustGet("user"),
			"CurrentPage": "customer-edit",
		})
	})

	// 客户新增页面
	pages.GET("/customers/new", middleware.RequireAnyPermission(db, []string{"customer.create"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "customers/new.html", gin.H{
			"title":       "新增客户",
			"user":        c.MustGet("user"),
			"CurrentPage": "customer-create",
		})
	})
}

// setupPartnerPages 设置合作机构管理页面路由
func setupPartnerPages(pages *gin.RouterGroup, db *gorm.DB) {
	// 合作机构管理页面
	pages.GET("/partners", middleware.RequireAnyPermission(db, []string{"partner.view", "partner.create", "partner.update", "partner.delete"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "partners/index.html", gin.H{
			"title":       "合作机构管理",
			"user":        c.MustGet("user"),
			"CurrentPage": "partners",
		})
	})

	// 合作机构新增页面
	pages.GET("/partners/new", middleware.RequireAnyPermission(db, []string{"partner.create"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "partners/form.html", gin.H{
			"title":       "新增合作机构",
			"user":        c.MustGet("user"),
			"mode":        "create",
			"CurrentPage": "partner-create",
		})
	})

	// 合作机构编辑页面
	pages.GET("/partners/edit", middleware.RequireAnyPermission(db, []string{"partner.update"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "partners/form.html", gin.H{
			"title":       "编辑合作机构",
			"user":        c.MustGet("user"),
			"mode":        "edit",
			"CurrentPage": "partner-edit",
		})
	})

	// 合作机构详情页面
	pages.GET("/partners/detail", middleware.RequireAnyPermission(db, []string{"partner.view"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "partners/detail.html", gin.H{
			"title":       "机构详情",
			"user":        c.MustGet("user"),
			"CurrentPage": "partner-detail",
		})
	})
}

// setupPushPages 设置推送管理页面路由
func setupPushPages(pages *gin.RouterGroup, db *gorm.DB, permissionMiddleware *middleware.PermissionMiddleware, cfg *config.Config) {
	// 创建服务
	userService := services.NewUserService(db, cfg)
	permissionService := services.NewPermissionService(db)

	// 创建控制器
	userCtrl := controllers.NewUserController(db, userService, permissionService)
	roleCtrl := controllers.NewRoleController(db, permissionService)
	permissionCtrl := controllers.NewPermissionController(db)
	systemCtrl := controllers.NewSystemController(db, permissionService)
	// 推送管理主页
	pages.GET("/push", middleware.RequireAnyPermission(db, []string{"push.manual", "push.view"}), func(c *gin.Context) {
		c.HTML(http.StatusOK, "push/index.html", gin.H{
			"title":       "推送管理",
			"user":        c.MustGet("user"),
			"CurrentPage": "push",
		})
	})

	// 手动推送页面
	pages.GET("/push/manual", permissionMiddleware.RequirePermission("push.manual"), func(c *gin.Context) {
		c.HTML(http.StatusOK, "push/manual.html", gin.H{
			"title":       "手动推送",
			"user":        c.MustGet("user"),
			"CurrentPage": "push-manual",
		})
	})

	// 推送记录页面
	pages.GET("/push/records", permissionMiddleware.RequirePermission("push.view"), func(c *gin.Context) {
		c.HTML(http.StatusOK, "push/records.html", gin.H{
			"title":       "推送记录",
			"user":        c.MustGet("user"),
			"CurrentPage": "push-records",
		})
	})

	// 推送统计页面
	pages.GET("/push/stats", permissionMiddleware.RequirePermission("push.stats"), func(c *gin.Context) {
		c.HTML(http.StatusOK, "push/stats.html", gin.H{
			"title":       "推送统计",
			"user":        c.MustGet("user"),
			"CurrentPage": "push-stats",
		})
	})

	// 用户管理页面
	userGroup := pages.Group("/system/users")
	userGroup.Use(permissionMiddleware.RequirePermissionForPage("system.user.view"))
	{
		userGroup.GET("", userCtrl.UsersIndex)
		userGroup.GET("/create", userCtrl.UserCreatePage)
		userGroup.GET("/edit/:id", userCtrl.UserEditPage)
		userGroup.GET("/detail/:id", userCtrl.UserDetailPage)
	}

	// 角色管理页面
	roleGroup := pages.Group("/system/roles")
	roleGroup.Use(permissionMiddleware.RequirePermissionForPage("system.role.view"))
	{
		roleGroup.GET("", roleCtrl.RolesIndex)
		roleGroup.GET("/create", roleCtrl.RoleCreatePage)
		roleGroup.GET("/edit/:id", roleCtrl.RoleEditPage)
		roleGroup.GET("/:id", roleCtrl.RoleDetailPage)
	}

	// 权限管理页面
	permissionGroup := pages.Group("/system/permissions")
	permissionGroup.Use(permissionMiddleware.RequirePermissionForPage("permission.view"))
	{
		permissionGroup.GET("", permissionCtrl.PermissionsIndex)
		permissionGroup.GET("/create", permissionCtrl.PermissionCreatePage)
		permissionGroup.GET("/edit/:id", permissionCtrl.PermissionEditPage)
		permissionGroup.GET("/detail/:id", permissionCtrl.PermissionDetailPage)
	}

	// 系统日志页面
	logsGroup := pages.Group("/system/logs")
	logsGroup.Use(permissionMiddleware.RequirePermissionForPage("system.log.view"))
	{
		logsGroup.GET("", systemCtrl.LogsIndex)
	}

}
